﻿//Copyright © Omega Point 2008
//All rights reserved.
//This Software is Licensed under the New BSD License
//for a full copy of this license see http://www.codeplex.com/networkmessenger/license
//Redistributions of source code must retain the above copyright notice.
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace NetworkMessenger
{
    internal static class Tools
    {
        /// <summary>
        /// Get the Port number which matches the specified
        /// </summary>
        /// <param name="Name">Name to find port for</param>
        /// <returns>Port Number</returns>
        public static int GetPortFromName(string name)
        {
            //Set Initial Port to Start of Dynamic Port Range
            int port = 49152;
            
            foreach (char ch in name)
            {
                int charNumber = (int)Char.ToUpper(ch);
                if (port + charNumber <= 65535)
                {                    
                    port = port + (int)ch;
                }
                else
                {
                    port = port - charNumber;
                }
            }
            return port;
        }

        /// <summary>
        /// Returns if there is more than 1 process with the specified name 
        /// </summary>
        /// <remarks>Currently unused</remarks>
        /// <param name="AppName">Process Name</param>
        /// <returns></returns>
        public static bool IsOnlyInstance(string appName)
        {
            Process[] processes = Process.GetProcessesByName(appName);
            if (processes.Length > 1)
            {              
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Adds User to Last Sent to List
        /// </summary>
        /// <param name="address">Address to Add</param>
        public static void AddToLastRecivers(string address)
        {
            if (Properties.Settings.Default.LastSentTo != null)
            {
                while (Properties.Settings.Default.LastSentTo.Count >= 10)
                {
                    Properties.Settings.Default.LastSentTo.RemoveAt(0);
                }
                if (Properties.Settings.Default.LastSentTo.Contains(address) == false)
                {
                    Properties.Settings.Default.LastSentTo.Add(address);
                }
            }
            else
            {
                Properties.Settings.Default.LastSentTo = new System.Collections.Specialized.StringCollection();
                Properties.Settings.Default.LastSentTo.Add(address);
            }
            Properties.Settings.Default.Save();
            
        }

        /// <summary>
        /// Upgrades any Old Settings to the Current Version
        /// </summary>
        public static void UpgradeSettings()
        {
            Properties.Settings.Default.Upgrade();

        }

        [DllImport("user32.dll")]
        public static extern bool FlashWindow(IntPtr hwnd, bool bInvert);       

    }
}
